package com.mlnx.data.service.iml;

import com.alibaba.fastjson.JSON;
import com.mlnx.data.dao.EcgStore;
import com.mlnx.data.entity.EcgInfo;
import com.mlnx.data.pojo.vo.EcgHeartVo;
import com.mlnx.data.service.EcgDataService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * Created by amanda.shan on 2019/10/27.
 */
@Service
public class EcgDataServiceIml implements EcgDataService {

    @Autowired
    private EcgStore ecgStore;

    @Override
    public boolean save(EcgInfo ecg) {
        return ecgStore.save(ecg);
    }

    @Override
    public boolean save(List<EcgInfo> ecgs) {
        return ecgStore.save(ecgs);
    }

    @Override
    public void delete(Integer patientId) {
        ecgStore.delete(patientId);
    }

    @Override
    public long count(Integer patientId) {
        return ecgStore.count(patientId);
    }

    @Override
    public List<EcgHeartVo> getEcgHeartVos(long startTime, long endTime, int patientId, Integer limit) {

        if (limit == null){
            limit = 100;
        }

        return ecgStore.getEcgHeartVos(startTime, endTime, patientId, limit);
    }

    @Override
    public List<Map> getEcg(long startTime, long endTime, int patientId, Integer limit) {
        return ecgStore.getEcg(startTime, endTime, patientId, limit);
    }

    @Override
    public List<Map> getEncryEcg(long startTime, long endTime, int patientId, Integer limit) {
        return ecgStore.getEncryEcg(startTime, endTime, patientId, limit);
    }

    @Override
    public List<Map> getFilterEcg(long startTime, long endTime, int patientId, Integer limit) {
        return ecgStore.getFilterEcg(startTime, endTime, patientId, limit);
    }

    @Override
    public List<Map> getFilterEcg(Integer patientId, Long startTime, Integer limit) {
        return ecgStore.getFilterEcg(patientId, startTime, limit);
    }

    @Override
    public List<Map> getEcg(Integer patientId, Long startTime, Integer limit) {
        return ecgStore.getEcg(patientId, startTime, limit);
    }

    @Override
    public List<Map> getEncryEcg(Integer patientId, Long startTime, Integer limit) {
        return ecgStore.getEncryEcg(patientId, startTime, limit);
    }

    @Override
    public List<Map> getLastEcgTime(Integer patientId, Long startTime, Integer limit) {
        return ecgStore.getLastEcgTime(patientId, startTime, limit);
    }

    @Override
    public void sendEcgData(Integer patientId, Long startTime, Long endTime, PrintWriter writer){
        sendEcgData(patientId, startTime, endTime, writer, "src");
    }

    @Override
    public void sendEncryEcgData(Integer patientId, Long startTime, Long endTime, PrintWriter writer){
        sendEcgData(patientId, startTime, endTime, writer, "encry");
    }

    @Override
    public void sendFilterEcgData(Integer patientId, Long startTime, Long endTime, PrintWriter writer){
        sendEcgData(patientId, startTime, endTime, writer, "filter");
    }

    @Override
    public Long getSaveTimeBySecond() {
        return ecgStore.getSaveTimeBySecond();
    }

    public void sendEcgData(Integer patientId, Long startTime, Long endTime, PrintWriter writer, String flag){
        StringBuffer sb = new StringBuffer();
        sb.append("{\"responseCode\":\"0000\",");
        sb.append("\"msg\":\"操作成功\",");
        sb.append("\"obj\":[");
        writer.write(sb.toString());

        while (true) {
            List<Map> data = null;
            if (flag.equals("src")) {
                data = getEcg(startTime, endTime, patientId, 4);
            } else if (flag.equals("encry")) {
                data = getEncryEcg(startTime, endTime, patientId, 4);
            } else if (flag.equals("filter")) {
                data = getFilterEcg(startTime, endTime, patientId, 4);
            }

            if (data != null && data.size() > 0) {
                String json = JSON.toJSONString(data);
                writer.write(json.substring(1, json.length() - 1));
                startTime = (Long) data.get(data.size() - 1).get("startTime") + 1L;
            }else {
                break;
            }
        }

        writer.write("]}");
    }
}
